﻿Imports OpenTK

Public Module SampleGenerator
    REM S = 4Pi*R^2
    REM V = 4Pi*R^3 / 3 

    Public Function MarsagliaRandomDirection(n As Integer) As Vector3()
        Dim u As Single
        Dim v As Single
        Dim x, y, z As Single
        Dim rand As New Random()
        Dim count = 0
        Dim result = New Vector3(n * n - 1) {}
        Do

            u = rand.Next(0, 65536) / 32768 - 1
            v = rand.Next(0, 65536) / 32768 - 1
            Dim r2 = u ^ 2 + v ^ 2
            If (r2 > 1) Then
                Continue Do
            End If
            x = 2 * u * Math.Sqrt(1 - r2)
            y = 2 * v * Math.Sqrt(1 - r2)
            z = 1 - 2 * r2
            result(count) = New Vector3(x, y, z)
            count += 1
        Loop While count < n ^ 2
        Return result
    End Function


End Module
